<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<html>
<head>
    <title>用户管理</title>
    <link rel="stylesheet" href="assets/ztree/zTreeStyle/zTreeStyle.css" type="text/css">
    <script type="text/javascript" src="assets/ztree/jquery.ztree.core.min.js"></script>
    <script type="text/javascript" src="assets/ztree/jquery.ztree.excheck.min.js"></script>
    <script type="text/javascript" src="assets/ztree/jquery.ztree.exedit.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="UserCtrl">
    <div class="col-md-3">
        <label>机构配置：</label>&nbsp;&nbsp
        <ui-tree url="system/user/checkedorg?code=${user.orgCode}" method ="get" selected="true" key="org"></ui-tree>
    </div>
    <div class="col-md-9">
        <ui-form key="userEdit" column="2" class="row">
            <label>角色配置：</label>&nbsp;&nbsp
            <div id="role">
            </div>
            <input type="hidden" name="id" id="id" value="${user.id}"/>
            <ui-form-input name="realName" label="用户姓名:" value="${user.realName}"></ui-form-input>
            <ui-form-input name="code" label="工号:"  value="${user.code}"></ui-form-input>
            <ui-form-input name="mobile" label="手机号:"  value="${user.mobile}"></ui-form-input>
            <ui-form-input name="idCard" label="身份证号:"  value="${user.idCard}"></ui-form-input>
            <ui-form-date label="入职日期" name="hireDate" value="${hireDate}"></ui-form-date>
            <ui-form-date label="离职日期" name="leaveDate" value="${leaveDate}"></ui-form-date>
            <ui-form-select name="onJob" label="在职状态:"  value="${user.onJob}">
                <option value="0">否</option>
                <option value="1">是</option>
            </ui-form-select>
            <ui-form-select name="settlementType" label="结算类型"  value="${user.settlementType}">
                <option value="0">投递员价</option>
                <option value="1">合作站点价</option>
                <option value="2">合作商价</option>
                <option value="3">大型合作商价</option>
            </ui-form-select>
            <ui-form-input label="新密码" name="passwd"></ui-form-input>
            <ui-form-input label="确认新密码" name="confirmPasswd"></ui-form-input>
        </ui-form>
    </div>
    <div class="row">
        <a class="btn btn-sm btn-success" onclick="back()">返回</a>
        <a class="btn btn-sm btn-primary" href="javascript:void(0);" onclick="changePasswd()">修改密码</a>
    </div>
<script>
    angular.module('myApp',['ui.tree','ui.form']);
    angular.module('myApp').controller('UserCtrl', function ($scope) {
        var url = "system/user/update";
        $scope.nodeData = {};
        $scope.$on('ui.form.submit',function (event,param) {
            var node = $scope['org'].getNodeByParam('selected',true);
            var passwd = param.passwd;
            var confirmPasswd = param.confirmPasswd;
            if(passwd != confirmPasswd){
                alert("两次输入密码不一样！");
                return;
            }
            if($scope['org']){
            var orgTree = $scope['org'].getSelectedNodes()||[];
            var orgCodes =[];
            for(var i =0 ;i<orgTree.length;i++){
                if(orgTree[i].code!=null){
                    orgCodes.push(orgTree[i].code);
                }
            }
            param.orgCodes = orgCodes.join(",");
            }
            var checkedValue=$("input:checked");
            var roleIds = [];
            for(var i = 0,l = checkedValue.length;i < l;i++) {
                roleIds.push(checkedValue[i].id)
            }
            param.roleIds = roleIds.join(",");
            $.ajax({
                url:url,
                type:"post",
                data:param
            }).done(function (result) {
                if(result.code == '0000'){
                    toastr.success("更新成功");
                    if(opener && opener.reloadTable){
                        opener.reloadTable({});
                    }
                    setTimeout(function () {
                        window.location.href="system/user/index.htm";
                    },1500);
                }else{
                    toastr.error(result.msg);
                }
            }).fail(function (err) {

            });
        });
    });

    $(function(){
        $("input[name='passwd']").hide();
        $("input[name='confirmPasswd']").hide();
        $("label:contains('新密码')").hide();
        $("label:contains('确认新密码')").hide();
        $.ajax({
            url:"system/user/listrole",
            type:"post",
            dataType:"json",
            async:false,
            success:function(data){
                if(data.code == "0000"){
                    showRole(data);
                }else{
                    alert(data.msg);
                }
            },
            error:function(XMLHttpRequest,text,error){
                alert("请求出错!");
                console.log("XMLHttpRequest"+XMLHttpRequest);
                console.log("text"+text);
                console.log("error"+error);
            }
        });

        $.ajax({
            url:"system/user/checkedrole?userCode=${user.code}",
            type:"get",
            dataType:"json",
            success:function(data){
                if(data.code == "0000"){
                    showChecked(data);
                }else{
                    alert(data.msg);
                }
            },
            error:function(XMLHttpRequest,text,error){
                alert("请求出错!");
                console.log("XMLHttpRequest"+XMLHttpRequest);
                console.log("text"+text);
                console.log("error"+error);
            }
        });
    });
    function showRole(data){
        var html = "";
        var roleList = data.data;
        for(var i = 0,l = roleList.length;i < l;i++){
            html += '<input id="'+roleList[i].id+'" name="multipleCheckbox" title="'+roleList[i].id+'" type="checkbox" onchange="change();"/>'+roleList[i].name+'&nbsp;&nbsp;';
            $("#role").html(html);
        }
    }
    function showChecked(data){
        var userRoleRels = data.data;
        for(var i = 0,l = userRoleRels.length;i < l;i++) {
            $("#"+userRoleRels[i].roleId).attr({
                checked:"checked"
            });
        }
    }
    function change(){
        var roleChecked = $("input[name=multipleCheckbox]:checked");
        var roleIds = "";
        roleChecked.each(function(index){
            roleIds += $(this).attr("title");
            roleIds += ",";
        });
        $("#roleIds").val(roleIds);
    }
    function changePasswd(){
        $("input[name='passwd']").show();
        $("input[name='confirmPasswd']").show();
        var passwd = $("label:contains('新密码')");
        var confirmPasswd = $("label:contains('确认新密码')");
        passwd.show();
        confirmPasswd.show();
        confirmPasswd.onkeyup = function confirm(passwd,confirmPasswd){
            if(!passwd == confirmPasswd){
                alert("两次输入密码不一样！");
                return;
            }
        }
    }
    function back(){
        window.location.href="system/user/index.htm";
    }
</script>
</div>
</body>
</html>

<script type="text/javascript">

</script>
